#include "stable.h"

#include "QkBaseView.h"
#include "QkViewManager.h"

QkViewManager::QkViewManager(QWidget *parent) : QWidget(parent), m_currentIndex(-1)
{
}

QkViewManager::~QkViewManager()
{
}

int QkViewManager::addView(QkBaseView *view)
{
	view->setParent(this);
	view->hide();
	m_viewList.append(view);

	return m_viewList.count() - 1;
}

void QkViewManager::setActiveView(QkBaseView *view)
{
	if(m_currentIndex != -1)
	{
		m_viewList[m_currentIndex]->hide();
		m_viewList[m_currentIndex]->deactivated();
	}

	m_currentIndex = m_viewList.indexOf(view);
	
	if(view->testAttribute(Qt::WA_PendingResizeEvent))
	{
		view->setGeometry(this->contentsRect());
	}
	view->show();
	m_viewList[m_currentIndex]->activated();
}

int QkViewManager::currentIndex() const
{
	return m_currentIndex;
}

int QkViewManager::count() const
{
	return m_viewList.count();
}

QkBaseView *QkViewManager::viewAt(int index)
{
	return m_viewList[index];
}

void QkViewManager::resizeEvent(QResizeEvent *event)
{
	if(m_currentIndex != -1)
	{
		for(int i = 0; i < count(); i++)
		{
			if(i == m_currentIndex)
			{
				viewAt(i)->setGeometry(QRect(QPoint(0, 0), event->size()));
			}
			else
			{
				viewAt(i)->setAttribute(Qt::WA_PendingResizeEvent);
			}
		}
	}
	QWidget::resizeEvent(event);
}